package com.mission.schedule.my160920.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.EditText;

/**
 * Created by liwei on 2017/2/7.
 */

public class MEditText extends EditText {
    public MEditText(Context context) {
        super(context);
    }

    public MEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_ENTER)
        {

            keyCodeListenter.enter();

            return true;
        }
        if (keyCode == KeyEvent.KEYCODE_DEL){
            if (getText().toString().length() == 0){
                keyCodeListenter.delete();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    private KeyCodeListenter keyCodeListenter;

    public void setKeyCodeListenter(KeyCodeListenter keyCodeListenter) {
        this.keyCodeListenter = keyCodeListenter;
    }

    public interface KeyCodeListenter{
        void enter();
        void delete();
    }
}
